//
//  STFOAuthViewController.swift
//  高仿新浪微博
//
//  Created by 沈腾飞 on 2017/1/19.
//  Copyright © 2017年 shen. All rights reserved.
//

import UIKit
import YYModel

class STFOAuthViewController: UIViewController {
    
    override func loadView() {
        view = webView
        
        // urlString
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(APPKEY)&redirect_uri=\(REDIRECT_URI)"
        // 实例化url
        let url = URL(string: urlString)
        guard let u = url else {
            return
        }
        // 实例化requet
        let request = URLRequest(url: u)
        // 加载
        webView.loadRequest(request)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        view.backgroundColor = UIColor.white
        setupNav()
        
    }
    
    //MARK: - 设置导航
    private func setupNav(){
        title = "微博登录"
        navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: nil, title: "取消", target: self, action: #selector(cancelClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: nil, title: "自动填充", target: self, action: #selector(autofillClick))
    }
    
    //MARK: - 懒加载控件
    // webview
    fileprivate lazy var webView: UIWebView = {
        let view = UIWebView()
        
        //为了截取code码  设置代理
        view.delegate = self
        
        return view
    }()
    
}

//实现代理
extension STFOAuthViewController : UIWebViewDelegate {
    
    //开始加载
    func webViewDidStartLoad(_ webView: UIWebView) {
        
    }
    //加载完成
    func webViewDidFinishLoad(_ webView: UIWebView) {
        
    }
    //webview将要加载的请求 会走该方法 默认返回true 
    //如果不实现该代理  默认会允许加载
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        //将要加载请求的字符串
        let urlString = request.url?.absoluteString
        //判断URLstring是否为nil 且 字符串前缀是不是回调页 
        if let u = urlString, u.hasPrefix(REDIRECT_URI) {
            //  请求参数部分
            let query = request.url?.query
            
            //判断query是不是nil
            if let q = query {
                print(q)
                //截取code码
                let code = q.substring(from: "code=".endIndex)
                print("截取到的code码",code)
                
                //发送请求  获取accessToken
                STFOAuthViewModel.shared.getUserAccount(code: code, isFinish: { (isFinish) in
                    if !isFinish {
                        print("失败")
                        return
                    } else {
                        print("成功")
                        
                    }
                })
            }
        }
        return true
    }
    
}






//MARK: - 监听事件
extension STFOAuthViewController {
    // 返回
    @objc fileprivate func cancelClick(){
        dismiss(animated: true, completion: nil)
    }
    // 自动填充
    @objc fileprivate func autofillClick(){
        print("自动填充")
        
        //JS 
        let jsString = "document.getElementById('userId').value='\(STFWBNAME)';document.getElementById('passwd').value='\(STFWBPASSWD)';"
        
        //js注入
        webView.stringByEvaluatingJavaScript(from: jsString)
        
        
    }
}
